package org.xurui.sxl.protocol.command.impl;

import org.jetlinks.core.message.DeviceMessage;
import org.jetlinks.core.message.codec.FromDeviceMessageContext;
import org.jetlinks.core.message.property.ReportPropertyMessage;
import org.xurui.sxl.protocol.command.CmdCodec;
import org.xurui.sxl.protocol.enums.CommandEnum;
import org.xurui.sxl.protocol.enums.MncEnum;
import org.xurui.sxl.protocol.utils.NumberUtils;
import org.xurui.sxl.protocol.utils.SxlMessageUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 心跳数据
 */
public class CmdCodec_KA implements CmdCodec {
    @Override
    public boolean supportsCmd(String cmd) {
        return CommandEnum.KA.name().equals(cmd);
    }

    @Override
    public boolean supportsRelayCmd() {
        return false;
    }

    @Override
    public List<DeviceMessage> parsePayload(String payload, FromDeviceMessageContext context) {
        //格式：*XX,YYYYYYYYYYYYYYY,KA,HHMMSS,Signal,SV,BAT,MCC,MNC,TA,NUM,LAC,CELLID,RXLEV…,DDMMYY,tracker_status#
        //示例1：*TJ,866104040001458,KA,130305,60,00,53,460,00,0,1,13831,72166777,0,220912,FFFFFFFF#
        //示例2：*TJ,866104040001458,KA,130305,60,00,53,460,00,0,2,13831,72166777,0,18409,45124710,0,220912,FFFFFFFF#
        //回复格式：*XX,YYYYYYYYYYYYYYY,V4,KA,seq,HHMMSS#
        //回复示例：*TJ,866104040001458,V4,KA,130305,130305#
        //解析数据为属性上报
        ReportPropertyMessage report = ReportPropertyMessage.create();
        String[] payloadArr = payload.replaceAll("\\*", "").split(",");
        String deviceId = payloadArr[1];
        report.setDeviceId(deviceId);
        report.setCode(CommandEnum.KA.name());
        report.setMessageId(deviceId + "_" + payloadArr[3]);
        report.setTimestamp(System.currentTimeMillis());
        Map<String, Object> properties = new HashMap<>();
        properties.put("uptime",payloadArr[3]);
        properties.put("signal", NumberUtils.parseInt(payloadArr[4]));
        properties.put("sv", NumberUtils.parseInt(payloadArr[5]));
        properties.put("bat", NumberUtils.parseInt(payloadArr[6]));
        Map ka = new HashMap();
        properties.put("ka",ka);
        //ka.put("mcc","460".equals(payloadArr[7])?"中国":payloadArr[7]);//移动设备国家代码，中国 460
        ka.put("mcc",payloadArr[7]);
        String mnc = payloadArr[8];
    MncEnum mncEnum = MncEnum.matchCode(mnc);
        //ka.put("mnc", mncEnum==null?mnc:mncEnum.getName());//移动设备网络代码，中国移动 = 00，中国联通 = 01,中国电信 = 11。
        ka.put("mnc", mnc);
        ka.put("ta",payloadArr[9]);
        int kaNumber = Integer.parseInt(payloadArr[10]);//数量，1-6，最多 6 个
        List<Map> kaInfo = new ArrayList<>();
        for (int i = 0; i < kaNumber; i++) {
            Map k = new HashMap();
            k.put("lac", payloadArr[11 + i * 3]);
            k.put("cellid", payloadArr[12 + i * 3]);
            k.put("rxlev", payloadArr[13 + i * 3]);
            kaInfo.add(k);
        }
        ka.put("kainfo", kaInfo);
        properties.put("upday", payloadArr[11 + kaNumber * 3]);

        String trackerStatusHex = payloadArr[11 + kaNumber * 3 + 1];
        Map<String, Object> trackerStatusMap = parseTrackerStatus(trackerStatusHex);
        properties.put("trackerstatus", trackerStatusMap);
        report.setProperties(properties);
        //回复设备数据
        //TODO 暂时没找到设备回复消息的方法
//        String time = DateFormatUtils.format(new Date(),"HHmmss");
//        String reply="*"+payloadArr[0]+","+deviceId+",V4,"+CommandEnum.KA.name()+","+payloadArr[3]+","+time+"#";
//        //中文 *
//        //英文 *
//        context.getSession()
//            .send(EncodedMessage
//                .simple(Unpooled.wrappedBuffer(reply.getBytes())))
//            .subscribe();
        SxlMessageUtils.bulidAndSend2DeviceV4Msg(context,payloadArr[0],deviceId,CommandEnum.KA.name(),payloadArr[3]);
        return genDeviceMessage(report);
    }

}
